{
LANG : PASCAL
PROB : runround
}

program RUNROUND;
const
  fi = 'runround.in';
  fo = 'runround.out';
type
  num = array[1..10] of shortint;
var
  free : array[1..9] of boolean;
  d, sm : num;
  l, m : longint;
  done : boolean;
  s : string;

function rr : boolean;
var
  t : num;
  i, j, s : longint;
begin
  rr := false;
  t := d;
  i := t[1] mod l + 1;
  s := 1;
  t[1] := -1;
  while (i <> 1) or (s < l) do
  begin
    if t[i] = -1 then exit;
    j := (i + t[i] mod l - 1) mod l+1;
    t[i] := -1;
    i := j;
    inc(s);
  end;
  rr := true;
end;

procedure out;
var
  i : integer;
begin
  for i := 1 to l do write(d[i]);
  writeln;
  done := true;
end;

function bigger : boolean;
var
  i : integer;
begin
  if l > length(s) then
  begin
    bigger := true;
    exit;
  end;
  for i := 1 to l do
    if d[i] < sm[i] then
    begin
      bigger := false;
      exit;
    end else
    if d[i] > sm[i] then
    begin
      bigger := true;
      exit;
    end;
  bigger := false;
end;

procedure try(k : integer);
var
  i : integer;
begin
  if k = l+1 then
  begin
    if bigger and rr then out;
    exit;
  end;
  if (k = 1) and (l = length(s)) then
  begin
    for i := sm[1] to 9 do
    begin
      free[i] := false;
      d[1] := i;
      try(k+1);
      if done then exit;
      free[i] := true;
    end;
  end
  else
  for i := 1 to 9 do
    if free[i] then
    begin
      free[i] := false;
      d[k] := i;
      try(k+1);
      if done then exit;
      free[i] := true;
    end;
end;

procedure process;
var
  i : integer;
begin
  str(m, s);
  for i := 1 to length(s) do sm[i] := ord(s[i]) - 48;
  fillchar(free, sizeof(free), true);
  fillchar(d, sizeof(d), 0);
  done := false;
  l := length(s);
  try(1);
  if done then exit;
  l := l + 1;
  try(1);
end;

begin
  assign(input, fi); reset(input);
  assign(output, fo); rewrite(output);
  read(m);
  process;
  close(output);
  close(input);
end.